/*global uni plus*/

const uniApp = {
  toast(message = '') {
    // #ifdef APP-PLUS
    plus.nativeUI.toast(message, {
      align: "center",
      duration: "short"
    });
    // #endif
    // #ifndef APP-PLUS
    uni.showToast({
      title: message,
      icon: "none"
    });
    // #endif
  },
  alert(
    message = '',
    alertCB = null,
    title = "",
    btnText = "确定"
  ) {
    // #ifdef APP-PLUS
    plus.nativeUI.alert(message, alertCB, title, btnText);
    // #endif
    // #ifndef APP-PLUS
    uni.showModal({
      title,
      content: message,
      showCancel: false,
      confirmText: btnText,
      success: res => {
        res.confirm && typeof alertCB === "function" && alertCB();
      }
    });
    // #endif
  },
  // 设置系统剪贴板的内容(setClipboardData)
  // 拓展:支持H5
  copy(content, success = null, fail = null) {
    // #ifndef H5
    uni.setClipboardData({
      data: content,
      success() {
        typeof success === "function" && success();
      },
      fail() {
        typeof fail === "function" && fail();
      }
    });
    // #endif
    // #ifdef H5
    let text = document.createElement("input");
    text.focus();
    text.setAttribute("value", content);
    document.body.appendChild(text);
    text.select();
    let result = document.execCommand("copy");
    document.body.removeChild(text)
    if (result) {
      typeof success === "function" && success();
    } else {
      typeof fail === "function" && fail();
    }
    // #endif
  },
  // 动态控制下载刷新开关,仅支持app
  pullDownRefresh() {
    const pages = getCurrentPages();
    const page = pages[pages.length - 1];
    const currentWebView = page.$getAppWebview();
    return {
      open: function() {
        currentWebView.setStyle({
          pullToRefresh: {
            support: true,
            style: plus.os.name === "Android" ? "circle" : "default"
          },
          bounce: "all"
        });
      },
      close: function() {
        currentWebView.setStyle({
          pullToRefresh: {
            support: false,
            style: plus.os.name === "Android" ? "circle" : "default"
          },
          bounce: "none"
        });
      }
    }
  }



};

export default uniApp;
